package com.ruoyi.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.bizsys.domain.WalletOrder;
import com.ruoyi.bizsys.service.IWalletOrderService;
import com.ruoyi.business.domain.WalletOrderVo;
import com.ruoyi.business.service.IWalletService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/financial")
public class FinancialManagementController {
    @Autowired
    private IWalletService iWalletService;

    @Autowired
    private IWalletOrderService walletOrderService;

    @Log(title = "财务审核", businessType = BusinessType.GET)
    @PostMapping("/management")
    public void financialManagement(@RequestBody JSONObject jsonObject) {
        log.info("调用业务系统进行审批---", jsonObject);
        String updateName = jsonObject.getString("updateName");
        String refusalReason = jsonObject.getString("refusalReason");
        List<String> serialNoArray = Arrays.asList(jsonObject.getString("serialNoArray").split(","));
        String approval = jsonObject.getString("approval");
        //s  流水号
        for (String orderSerialNo : serialNoArray) {
            try {
                WalletOrder walletOrder = walletOrderService.getOne(new QueryWrapper<WalletOrder>().eq("serial_no", orderSerialNo));
                WalletOrderVo walletOrderVo = new WalletOrderVo();
                walletOrderVo.setSerialNo(orderSerialNo);
                walletOrderVo.setCustNo(walletOrder.getCustNo());
                walletOrderVo.setOrderType(walletOrder.getOrderType());
                walletOrderVo.setWalletType(walletOrder.getWalletType());
                walletOrderVo.setLoanSerialNo(walletOrder.getLoanSerialNo());
                walletOrderVo.setOrderStatus(walletOrder.getOrderStatus());
                walletOrderVo.setAmount(walletOrder.getAmount());
                walletOrderVo.setBankCode(walletOrder.getBankCode());
                walletOrderVo.setBankAccount(walletOrder.getBankAccount());
                walletOrderVo.setRejectedReason(refusalReason);
                walletOrderVo.setReviewer(updateName);
                iWalletService.orderApproval(walletOrderVo, approval);
            } catch (Exception e) {
                log.error("财务审核异常：{} - {}", orderSerialNo, e.getMessage());
            }
        }
    }
}
